package cn.tedu.zx.ums.controller;

import cn.tedu.zx.common.restful.JsonPage;
import cn.tedu.zx.common.restful.JsonResult;
import cn.tedu.zx.pojo.users.param.*;
import cn.tedu.zx.pojo.users.vo.UserData;
import cn.tedu.zx.pojo.users.vo.UserListItemVO;
import cn.tedu.zx.pojo.users.vo.UserRemPasswordVO;
import cn.tedu.zx.pojo.users.vo.UserStandardVO;
import cn.tedu.zx.ums.service.IUserRegService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Api(tags = "01. 用户管理模块")
@Slf4j
@Validated
public class UserController {
    @Autowired
    private IUserRegService iUserRegService;

    // http://localhost:10002/user/add-new
    @PostMapping("/add-new")
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid UserRegAddNewParam userRegAddNewParam) {
        log.debug("开始处理【添加用户】的请求，参数：{}", userRegAddNewParam);
        iUserRegService.addNew(userRegAddNewParam);
        log.debug("处理【添加用户】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/updatePassword")
    @ApiOperation("根据ID修改用户密码")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@Range(min = 1, message = "根据ID查询用户详情失败，请提交合法的ID值！")
                                         @RequestParam Long id,
                                     @Valid UserUpdateInfoParam userUpdateInfoParam) {
        log.debug("开始处理【修改用户密码】的业务，id：{},数据：{}",id, userUpdateInfoParam);
        iUserRegService.updateInfoById(id,userUpdateInfoParam);
        log.debug("用户密码修改完成");
        return JsonResult.ok();
    }

    @GetMapping("/remPassword")
    @ApiOperation("忘记密码")
    @ApiOperationSupport(order = 350)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户手机号码", required = true, dataType = "String")
    })
    public JsonResult userRemPassword(@RequestParam String phone) {
        log.debug("开始处理【忘记密码】的请求，手机号码：{}", phone);
        UserRemPasswordVO result = iUserRegService.getUserRemPassword(phone);
        return JsonResult.ok(result);
    }

    @PostMapping("/updateRemPassword")
    @ApiOperation("验证码正确后修改密码")
    @ApiOperationSupport(order = 301)
    public JsonResult updateUserRemPassword(@Valid UserRemPasswordParam userRemPasswordParam) {
        log.debug("开始处理【忘记密码】的请求，手机号码：{}", userRemPasswordParam);
        iUserRegService.updateUserRemPassword(userRemPasswordParam);
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询用户详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询用户详情】的请求，参数：{}", id);
        UserStandardVO result = iUserRegService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("/list")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "页码", paramType = "Integer")
    })
    public JsonResult<JsonPage<UserListItemVO>> list(@RequestParam Integer page, @RequestParam Integer pageSize) {
        JsonPage<UserListItemVO> pageData = iUserRegService.list(page, pageSize);
        return JsonResult.ok(pageData);
    }

    @PostMapping("/updateIsEnable")
    @ApiOperation("修改用户is_enable")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "Long"),
            @ApiImplicitParam(name = "isEnable", value = "启用值", paramType = "Integer")
    })
    public JsonResult updateIsEnable(@RequestParam Long id, @RequestParam Integer isEnable) {
        iUserRegService.updateIsEnable(id, isEnable);
        return JsonResult.ok();
    }

    @PostMapping("/deleteById")
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 440)
    @ApiImplicitParam(name = "id", value = "id", paramType = "Long")
    public JsonResult deleteById(@RequestParam Long id) {
        iUserRegService.deleteById(id);
        return JsonResult.ok();
    }
    @PostMapping("updateData")
    @ApiOperation("根据ID修改用户数据")
    @ApiOperationSupport(order = 450)
    public JsonResult updateData(@Valid UserUpdateDataParam userUpdateDataParam){
        iUserRegService.updateDataById(userUpdateDataParam);
        return JsonResult.ok();
    }

    @PostMapping("updateAccount")
    @ApiOperation("修改用户账户")
    @ApiOperationSupport(order = 450)
    public JsonResult updateAccount(@Valid UserUpdateAccountParam userUpdateAccountParam){
        iUserRegService.updateAccountById(userUpdateAccountParam);
        return JsonResult.ok();
    }

    @GetMapping("getUserData")
    @ApiOperation("获取用户基础信息")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParam(name = "username", value = "用户名",required = true, paramType = "String")
    public JsonResult<UserData> getUserData(@RequestParam String username){
        UserData userData = iUserRegService.getUserData(username);
        return JsonResult.ok(userData);
    }
}


